package com.example.gmallproduct.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.gmallproduct.VO.AttrGroupVo;
import com.example.gmallproduct.VO.AttrReleationVo;
import com.example.gmallproduct.VO.AttrResponseVo;
import com.example.gmallproduct.dao.AttrAttrgroupRelationDao;
import com.example.gmallproduct.dao.AttrDao;
import com.example.gmallproduct.dao.AttrGroupDao;
import com.example.gmallproduct.entity.AttrAttrgroupRelationEntity;
import com.example.gmallproduct.entity.AttrEntity;
import com.example.gmallproduct.service.AttrAttrgroupRelationService;
import com.example.gmallproduct.service.AttrService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.example.gmallproduct.entity.AttrGroupEntity;
import com.example.gmallproduct.service.AttrGroupService;
import com.common.utils.PageUtils;
import com.common.utils.R;


/**
 * 属性分组
 *
 * @author WDL
 * @email 2329156214@QQ.com
 * @date 2022-05-01 15:44:58
 */
@RestController
@RequestMapping("gmallproduct/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;

    @Autowired
    private AttrService attrService;

    @Autowired
    private AttrAttrgroupRelationService relationService;


    /**
     * 获取分类下所有attGroup信息&以及相应分组的关联属性
     *
     * @return
     */
    @RequestMapping("/{catelogId}/withattr")
    public R getA(@PathVariable("catelogId") Long catelogId) {
        List<AttrGroupVo> attrs = attrGroupService.getAttrsBycatelogId(catelogId);
        return R.ok().put("data", attrs);
    }


    ///product/attrgroup/attr/relation
    @PostMapping("/attr/relation")
    public R addRelation(@RequestBody List<AttrReleationVo> vos) {

        relationService.saveBatch(vos);
        return R.ok();
    }

    @RequestMapping("/{attrgroupId}/noattr/relation")
    public R getNoAttrRelation(@RequestParam Map<String, Object> params, @PathVariable("attrgroupId") Long attrGroupId) {
        PageUtils page = attrService.findNoAttr(params, attrGroupId);
        return R.ok().put("page", page);
    }

    @RequestMapping("/attr/relation/delete")
    public R deleteRelation(@RequestBody AttrReleationVo[] relationMap) {
        attrGroupService.deleteRelation(relationMap);
        return R.ok();
    }


    @RequestMapping("/{attrgroupId}/attr/relation")
    public R relationGroup(@PathVariable("attrgroupId") Long attrgroupId) {

        List<AttrResponseVo> data = attrGroupService.getRelationAttr(attrgroupId);

        return R.ok().put("data", data);
    }

    /**
     * 列表
     * catId,三级分类号,如果为零表示查找所有
     */
    @RequestMapping("/list/{catId}")
    public R list(@RequestParam Map<String, Object> params, @PathVariable("catId") Long catId) {
//        PageUtils page = attrGroupService.queryPage(params);

        PageUtils page = attrGroupService.queryByCatId(params, catId);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    public R info(@PathVariable("attrGroupId") Long attrGroupId) {
//		AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
        AttrGroupEntity attrGroup = attrGroupService.getBySelf(attrGroupId);
        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AttrGroupEntity attrGroup) {
        attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrGroupEntity attrGroup) {
        attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] attrGroupIds) {
        attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

}
